<?php

class App_Model_DbTable_Recommend_Tag extends Ext_Db_Table  
{
    protected $_name = 'recommend_tag';

    private static $_recommendTagRow = array();
    
    public function getRowById($recommendTagId)
    {
        if(!$recommendTagId)
        {
            return "";
        }
        
        if(isset(self::$_recommendTagRow['id'][$recommendTagId]))
        {
            return self::$_recommendTagRow['id'][$recommendTagId];
        }
        
        $db = $this->getAdapter();
        $safe = $db->quoteInto(" id = ? ", $recommendTagId);
        $row = $this->fetchRow($safe);
        if (!$row) {
            return "";
        }
        $recommendTagRow = $row->toArray();

        self::$_recommendTagRow['id'][$recommendTagId] = $recommendTagRow;
        $tagId = $recommendTagRow['tagId'];
        if($tagId)
        {
            self::$_recommendTagRow['tagId'][$tagId] = $recommendTagRow;
        }
        
        return $recommendTagRow;
    }

    public function getRowByTagId($tagId)
    {
        if(!$tagId)
        {
            return "";
        }
        
        if(isset(self::$_recommendTagRow['tagId'][$tagId]))
        {
            return self::$_recommendTagRow['tagId'][$tagId];
        }
        
        $db = $this->getAdapter();
        $select = $db->select();
        $select
            ->from($this->_name, "*")
            ->order('sequence ASC')
            ->where('tagId = :tagId')
            ;
        $params = array('tagId' => $tagId);
        $select->bind($params);
        $row = $db->fetchRow($select);
        if (!$row) {
            return "";
        }
        $recommendTagRow = $row;

        self::$_recommendTagRow['tagId'][$tagId] = $recommendTagRow;
        $recommendTagId = $recommendTagRow['id'];
        if($recommendTagId)
        {
            self::$_recommendTagRow['id'][$recommendTagId] = $recommendTagRow;
        }
        
        return $recommendTagRow;
    }
    
    public function getMaxSequence()
    {
        $db = $this->getAdapter();
        $select = $db->select();
        $select
            ->from($this->_name, "MAX(sequence)")
            ;
        $sequence = $db->fetchOne($select);
        return $sequence;
    }

    public function del($recommendTagId)
    {
        $db = $this->getAdapter();
        $safe = $db->quoteInto(" id = ? ", $recommendTagId);
        $rows_affected = $this->delete($safe);
        if($rows_affected){
            $this->_unsetRow($recommendTagId);
        }
        return $rows_affected;
    }
        
    public function getAll()
    {
        $db = $this->getAdapter();
        $select = $db->select();
        $select
            ->from($this->_name, "*")
            ->order('sequence ASC')
            ;
        
        $list = $db->fetchAll($select);
        return $list;
    }
    
    public function addNew($inputArray)
    {
        $sql_data_array = array();
        if(array_key_exists('tagId', $inputArray)) $sql_data_array['tagId'] = $inputArray['tagId'];
        if(array_key_exists('sequence', $inputArray)) $sql_data_array['sequence'] = $inputArray['sequence'];

        try {
            // 插入数据行并返回插入的行数
            $rows_affected = $this->insert($sql_data_array);
            // 最后插入的数据id   
            $last_insert_id = $this->getAdapter()->lastInsertId();
        }catch (Exception $e){
            trigger_error($e->getMessage(), E_USER_WARNING);
        }
        return $last_insert_id;
    }
    
    public function addOld($inputArray, $recommendTagId)
    {
        $sql_data_array = array();
        if(array_key_exists('sequence', $inputArray)) $sql_data_array['sequence'] = $inputArray['sequence'];
        if(!$sql_data_array)
        {
            return "";
        }

        try {
            $db = $this->getAdapter();
            $where = $db->quoteInto(" id = ? ", $recommendTagId);
            $rows_affected = $this->update($sql_data_array, $where);
            if($rows_affected){
                $this->_unsetRow($recommendTagId);
            }
        }catch (Exception $e){
            trigger_error($e->getMessage(), E_USER_WARNING);
        }
    }
    
    private function _unsetRow($recommendTagId)
    {
        if(!$recommendTagId)
        {
            return "";
        }
        
        $recommendTag = $this->getRowById($recommendTagId);
        if(!$recommendTag)
        {
            return "";
        }
        $tagId = $recommendTag['tagId'];
        unset(self::$_recommendTagRow['id'][$recommendTagId]);
        unset(self::$_recommendTagRow['tagId'][$tagId]);
    }

}